from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QDialog, QPushButton, QFrame, QHBoxLayout
from PyQt5.QtCore import Qt, QSize
from config import frame_config, Text_config,button_config
from change_user import UpdateUser

class My(QDialog):
    def __init__(self, parent=None):
        super().__init__()
        self.parent = parent
        self.client=self.parent.client
        self.setWindowTitle('用户中心')
        self.resize(400,400)
        self.sf=self.parent.sf
        self.password=self.parent.password
        self.user_name=self.parent.user_name
        self.email=self.parent.email
        self.avatar=self.parent.avatar
        self.a=False
        self.initUI()
    def initUI(self):
        mainLayout = QVBoxLayout(self)

        frame=QFrame()
        mainLayout.addWidget(frame)
        layout1=QVBoxLayout(frame)
        self.img_label=QLabel()
        img=QPixmap()
        img.loadFromData(self.avatar)
        img = img.scaled(200, 200, Qt.KeepAspectRatio)
        self.img_label.setPixmap(img)
        self.img_label.setAlignment(Qt.AlignCenter)
        layout1.addWidget(self.img_label)

        layout1=QHBoxLayout()
        mainLayout.addLayout(layout1)
        self.label_username=QLabel()
        text = f'用户名:{self.user_name}'
        self.label_username.setText(text)
        self.label_username.setStyleSheet(Text_config.text_style2)
        layout1.addWidget(self.label_username,alignment=Qt.AlignCenter)

        layout1=QHBoxLayout()
        mainLayout.addLayout(layout1)
        self.label_email=QLabel()
        text = f'用户邮箱:{self.email}'
        self.label_email.setText(text)
        self.label_email.setStyleSheet(Text_config.text_style2)
        layout1.addWidget(self.label_email,alignment=Qt.AlignCenter)

        layout1 = QHBoxLayout()
        mainLayout.addLayout(layout1)
        self.label = QLabel()
        text = f'当前身份:{self.sf}'
        self.label.setText(text)
        self.label.setStyleSheet(Text_config.text_style2)
        layout1.addWidget(self.label, alignment=Qt.AlignCenter)
        btn_sf=QPushButton()
        btn_sf.setText('更换身份')
        btn_sf.setStyleSheet(button_config.button_style2)
        layout1.addWidget(btn_sf)

        layout1 = QHBoxLayout()
        mainLayout.addLayout(layout1)
        btn_change=QPushButton()
        btn_change.setText('修改信息')
        btn_change.setStyleSheet(button_config.button_style1)
        layout1.addWidget(btn_change)
        btn_exit=QPushButton()
        btn_exit.setText('返回')
        btn_exit.setStyleSheet(button_config.button_style4)
        layout1.addWidget(btn_exit)

        btn_exit.clicked.connect(self.exit)
        btn_sf.clicked.connect(self.sf_cg)
        btn_change.clicked.connect(self.change)

    def sf_cg(self):
        if self.sf=='client':
            self.parent.sf='seller'
            self.a=False
            self.sf='seller'
            text = f'当前身份:{self.sf}'
            self.label.setText(text)
        else:
            self.parent.sf='client'
            self.a=True
            self.sf='client'
            text = f'当前身份:{self.sf}'
            self.label.setText(text)

    def change(self):
        up = UpdateUser(self)
        up.exec_()
        self.parent.user_name=self.user_name
        self.parent.email=self.email
        self.parent.password=self.password
        self.parent.avatar=self.avatar
        img=QPixmap()
        img.loadFromData(self.avatar)
        img = img.scaled(200, 200, Qt.KeepAspectRatio)
        self.img_label.setPixmap(img)
        text = f'用户名:{self.user_name}'
        self.label_username.setText(text)
        text=f'用户邮箱:{self.email}'
        self.label_email.setText(text)
    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()

    def closeEvent(self, event):
        if self.a:
            self.client.add_online(self.user_name)

